package city.spring.modules.work.controller;

import city.spring.modules.work.entity.WorkPlanShiftExtEntity;
import city.spring.modules.work.repository.WorkPlanShiftExtRepository;
import city.spring.modules.work.service.WorkPlanShiftExtService;
import city.spring.modules.work.vo.WorkPlanShiftVO;
import com.baomidou.mybatisplus.extension.api.ApiController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 排班方案的上班班次管理
 *
 * @author HouKunLin
 * @date 2020/6/7 0007 16:42
 */
@RestController
@RequestMapping("/work_plan/{planId}/shift")
public class WorkPlanShiftController extends ApiController {
    private final WorkPlanShiftExtRepository planShiftExtRepository;
    private final WorkPlanShiftExtService planShiftExtService;

    public WorkPlanShiftController(WorkPlanShiftExtRepository planShiftExtRepository, WorkPlanShiftExtService planShiftExtService) {
        this.planShiftExtRepository = planShiftExtRepository;
        this.planShiftExtService = planShiftExtService;
    }

    @ModelAttribute
    public void getPlanId(@PathVariable String planId) {
        if (planId == null) {
            throw new RuntimeException("排班方案ID必须不为空");
        }
    }

    /**
     * 获取排班方案所有班次列表信息
     *
     * @param planId 方案ID
     */
    @GetMapping
    public Object list(@PathVariable String planId) {
        return success(planShiftExtRepository.getPlanShifts(planId));
    }

    /**
     * 给方案添加多个班次信息
     *
     * @param planId 方案ID
     * @param shifts 所添加的班次信息列表
     * @return
     */
    @PostMapping
    public Object addAll(@PathVariable String planId, @RequestBody List<WorkPlanShiftExtEntity> shifts) {
        // 查出已有的数据
        List<WorkPlanShiftVO> dbShifts = planShiftExtRepository.getPlanShifts(planId);
        // 数据库已经存在的班次列表
        Set<String> dbShiftIdsSet = dbShifts.stream()
                .map(WorkPlanShiftExtEntity::getWorkShiftId)
                .collect(Collectors.toSet());
        // 过滤掉已经存在数据库的数据
        List<WorkPlanShiftExtEntity> needSaveShifts = shifts.stream()
                .filter(item -> {
                    item.setWorkPlanId(planId);
                    String workShiftId = item.getWorkShiftId();
                    // 过滤出不存在数据库中的数据
                    return workShiftId != null && !dbShiftIdsSet.contains(workShiftId);
                }).collect(Collectors.toList());
        if (!needSaveShifts.isEmpty()) {
            planShiftExtService.saveBatch(needSaveShifts);
        }

        return success(null);
    }

    /**
     * 获取方案的一个上班信息
     *
     * @param planId 方案ID
     * @param extId  关联ID
     * @return
     */
    @GetMapping("{extId}")
    public Object getInfo(@PathVariable String planId, @PathVariable Long extId) {
        return success(planShiftExtRepository.getPlanShift(planId, extId));
    }

    /**
     * 修改一个方案关联的班次信息
     *
     * @param planId    方案ID
     * @param extId     关联ID
     * @param extEntity 接收的数据
     * @return
     */
    @PutMapping("{extId}")
    public Object update(@PathVariable String planId, @PathVariable Long extId, @RequestBody WorkPlanShiftExtEntity extEntity) {
        extEntity.setId(extId);
        extEntity.setWorkPlanId(planId);
        planShiftExtService.updateWorkPlanShiftExt(extEntity);
        return success(null);
    }

    /**
     * 删除一个 <strong>排班方案关联班次信息</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        planShiftExtService.deleteWorkPlanShiftExt(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>排班方案关联班次信息</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        planShiftExtService.deleteWorkPlanShiftExt(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
